<?php
//==============================================
// 文件名称       _Global.php
// 代码所有       PHPText.Net
// 代码作者       黄创钦,tine2cx@163.com
// 联系方式       QQ 233944397
// 本代码开源使用，可以随意修改传播，但请保留本版权
// 声明,谢谢合作！
//==============================================

	if(!_NavLV_){ die(); }

	header("Content-type:text/html;charset=utf-8");

	//导入模块
	require '_Include/TextDB.ELib.php';  //文本数据库
	require '_Include/PageFT.ELib.php';  //分页函数
	require '_Include/System.php';  //系统模块
	require 'Config.php'; //导入论坛配置

	TextDB_Init();  //启动 数据库引擎

	$Install = 0;
	//存在数据库
	if(TextDB_CheckDB(_ForumDBName_))
	{
		TextDB_ConnectDB(_ForumDBName_);                   //链接论坛数据库
		TextDB_CheckTable('Config')        && $Install++;  //检测表 Config
		TextDB_CheckTable('Members')       && $Install++;  //检测表 Members
		TextDB_CheckTable('Skins')         && $Install++;  //检测表 Skins
		TextDB_CheckTable('TextLinks')     && $Install++;  //检测表 TextLinks
		TextDB_CheckTable('PicLinks')      && $Install++;  //检测表 PicLinks
		TextDB_CheckTable('Board')         && $Install++;  //检测表 Board
		TextDB_CheckTable('SecurityCode')  && $Install++;  //检测表 SecurityCode
		TextDB_CheckTable('OnlineInfo')    && $Install++;  //检测表 OnlineInfo
		TextDB_CheckTable('Announcements') && $Install++;  //检测表 Announcements
		TextDB_CheckTable('ALL_TopicList') && $Install++;  //检测表 ALL_TopicList
		TextDB_CheckTable('ALL_ReplyList') && $Install++;  //检测表 ALL_ReplyList
		TextDB_CheckTable('ALL_TRList')    && $Install++;  //检测表 ALL_TRList
	}

	//如果数据表不完整
	if($Install<12 || $Install==0)
	{
		echo "
		<html>
		<head>
		<meta http-equiv='content-type' content='text/html; charset=utf-8' />
		<meta http-equiv='content-language' content='utf-8' />
		<meta http-equiv='refresh' content='3; url=Install.php'>
		<title>跳转：TextDB Forum beta安装页面</title>
		</head>
		<body style='background:#ccc;'>
			<table width='100%' height='100%'>
			<tr><td align='center' valign='center'>
			<table border='0' cellpadding='0' cellspacing='0' width='300'>
				<tr><td align='center' valign='center' style='background:#000;color:#fff;border:#000 1px solid;border-bottom:none;padding:5px;'><b>TextDB Forum 提示</b></td></tr>
				<tr><td align='center' valign='center' height='36' style='font-size:12px;background:#fff;color:#000;border:#000 1px solid;'>论坛还未进行安装,将在3秒后，自动跳转到安装页面</td></tr>
			</table>
			</td></tr></table>
		</body>
		</html>
		";
		die();  //停止脚本
	}

	//清除在线记录
	CleanOnlineSave();

	//获取访问ID号
	if(isset($_COOKIE['SID']) && !empty($_COOKIE['SID'])) //存在访问ID
	{
		define('_SID_',$_COOKIE['SID']);
	}
	else //不存在访问ID，则获取并记录
	{
		$_SID = GetSID();
		setcookie('SID',$_SID,time()+3600);
		define('_SID_',$_SID);
	}

	//获取客户信息
	define('_ClinetIP_',      GetClinetIP());
	define('_ClinetOS_',      GetClinetOS());
	define('_ClinetBrowser_', GetClinetBrowser());

	//检测是否登录
	$OnlineInfo = TextDB_SearchData('OnlineInfo','认证号',_SID_);
	if($OnlineInfo)  //如果存在登录信息
	{
		$MemberInfo = TextDB_SearchData('Members','TEXTDB_HID',$OnlineInfo[0]['用户编号']);

		//获取用户信息
		if($MemberInfo)
		{
			$Jobs  = $MemberInfo[0]['权限'];
			$Level = $MemberInfo[0]['等级'];
		}
		else
		{
			$Jobs  = 0;
			$Level = 0;
		}

		//更新状态
		TextDB_ChangeData('OnlineInfo','认证号',_SID_,'岗位,等级,位置,创建时间',array($Jobs,$Level,_ForumUrl_.$_SERVER['PHP_SELF'],time()));

		define('_LoginName_',       $OnlineInfo[0]['名称']);      //定义登录用户名
		define('_LoginID_',         $OnlineInfo[0]['用户编号']);  //定义登录用户ID
		define('_LoginJobs_',       $Jobs);                      //定义用户岗位
		define('_LoginLevel_',      $Level);                     //定义用户等级
		define('_LoginPOS_',        $OnlineInfo[0]['位置']);     //定义用户位置
		define('_LoginOS_',         $OnlineInfo[0]['操作系统']); //定义用户操作系统
		define('_LoginBrowser_',    $OnlineInfo[0]['浏览器']);   //定义用户浏览器
		define('_LoginIP_',         $OnlineInfo[0]['访问IP']);   //定义用户访问IP
		define('_LoginDate_',       $OnlineInfo[0]['访问时间']); //定义用户访问时间

	}
	else  //如果未登录
	{
		//写入登录信息
		TextDB_InsertData('OnlineInfo','*',array(_SID_,0,'游客',0,0,_ForumUrl_.$_SERVER['PHP_SELF'],_ClinetOS_,_ClinetBrowser_,_ClinetIP_,time(),time()));

		define('_LoginName_',    '游客');               //定义登录用户名
		define('_LoginID_',      0);                    //定义登录用户ID
		define('_LoginJobs_',    0);                    //定义用户岗位
		define('_LoginLevel_',   0);                    //定义用户等级
		define('_LoginPOS_',     $_SERVER['PHP_SELF']); //定义用户位置
		define('_LoginOS_',      _ClinetOS_);           //定义用户操作系统
		define('_LoginBrowser_', _ClinetBrowser_);      //定义用户浏览器
		define('_LoginIP_',      _ClinetIP_);           //定义用户访问IP
		define('_LoginDate_',    time());  //定义用户访问时间
	}

	if($OnlineInfo[0]['岗位'])  //有岗位
	{
		define('_UserLogin_',true);  //设置为登录模式
		$MembersInfo = TextDB_SearchData('Members','昵称',_LoginName_);
	}
	else  //没有岗位
	{
		define('_UserLogin_',false);  //设置为未登录模式
	}

	//获取上次登陆信息
	if(!empty($MembersInfo[0]['上次访问IP']))
	{
		define('_LastLoginIP_',   $MembersInfo[0]['上次访问IP']); //定义用户上次访问IP
		define('_LastLoginDate_', date('Y-m-d H:i:s',$MembersInfo[0]['上次访问时间'])); //定义用户上次访问时间
	}
	else
	{
		define('_LastLoginIP_',   '没有记录'); //定义用户上次访问IP
		define('_LastLoginDate_', '没有记录'); //定义用户上次访问时间
	}

	$ServerDate = time();                             //获取服务器时间
	$CNWeek    = array('日','一','二','三','四','五','六');  //设置日期
	$DateWeek  = '星期'.$CNWeek[date('w')];                 //获取星期几

	if(_UserLogin_)  //如果登录
	{
		//顶部菜单
		$Menu[0]['Url']   = 'profile.php?action=view&uid='._LoginID_;
		$Menu[0]['Name']  = _LoginName_.' 控制面板';
		$Menu[0]['Title'] = '进入用户控制面板';

		$Menu[1]['Url']   = 'profile.php?action=list';
		$Menu[1]['Name']  = '会员';
		$Menu[1]['Title'] = '查看注册会员';

		$Menu[2]['Url']   = 'login.php?action=quit';
		$Menu[2]['Name']  = '退出';
		$Menu[2]['Title'] = '退出登录';

		//下拉菜单
		$NavMain[0]['Url']  = 'profile.php?action=view&uid='._LoginID_;
		$NavMain[0]['Name'] = '进入控制面板';

		$NavMain[1]['Url']  = 'profile.php?action=list';
		$NavMain[1]['Name'] = '会员列表/信息';

		$NavMain[2]['Url']  = 'login.php?action=quit';
		$NavMain[2]['Name'] = _LoginName_.' 退出';
	}
	else  //如果没有登录
	{
		//顶部菜单
		$Menu[0]['Url']   = 'register.php';
		$Menu[0]['Name']  = '注册新用户';
		$Menu[0]['Title'] = '进入注册页面';

		$Menu[1]['Url']   = 'login.php';
		$Menu[1]['Name']  = '登录';
		$Menu[1]['Title'] = '进入登录页面';

		if(_MemberInfoDisply_)
		{
			$Menu[2]['Url']   = 'profile.php?action=list';
			$Menu[2]['Name']  = '会员';
			$Menu[2]['Title'] = '查看注册会员';
		}

		//下拉菜单
		$NavMain[0]['Url']  = 'login.php';
		$NavMain[0]['Name'] = '登录';

		$NavMain[1]['Url']  = 'register.php';
		$NavMain[1]['Name'] = '注册新用户';

		if(_MemberInfoDisply_)
		{
			$NavMain[2]['Url']  = 'profile.php?action=list';
			$NavMain[2]['Name'] = '会员列表/信息';
		}
	}

	$BoardInfo = TextDB_SearchData('Board');  //获取分类信息
	$NavID     = count($NavMain)-1;
	if($BoardInfo)
	{
		//获取板块信息
		foreach($BoardInfo as $Board)
		{
			$B_ID = $Board['TEXTDB_HID'];
			$NavID++;
			$NavMain[$NavID]['Url']  = 'viewboard.php?bid='.$B_ID;
			$NavMain[$NavID]['Name'] = '≡ '.$Board['分类名称'].' ≡';

			//获取论坛信息
			$ForumInfo               = TextDB_SearchData('Forum_B'.$B_ID);
			if($ForumInfo)
			{
				$NavIDNum = 0;
				foreach($ForumInfo as $K=>$Forum)
				{
					$F_ID = $Forum['TEXTDB_HID'];
					$NavMain[$NavID]['Forum'][$K]['Url']  = 'viewforum.php?bid='.$B_ID.'&fid='.$F_ID;
					$NavMain[$NavID]['Forum'][$K]['Name'] = $Forum['论坛名称'];
				}
			}
		}
	}

	$action = isset($_GET['action']) ? $_GET['action'] : '';
	$bid    = isset($_GET['bid']) ? $_GET['bid'] : '';
	$fid    = isset($_GET['fid']) ? $_GET['fid'] : '';
	$tid    = isset($_GET['tid']) ? $_GET['tid'] : '';
	$uid    = isset($_GET['uid']) ? $_GET['uid'] : '';
	$Nav    = array();

	//如果浏览板块，论坛，主题 输出板块信息
	if(_NavLV_ == 'ViewBoard' || _NavLV_ == 'ViewForum' || _NavLV_ == 'ViewTopic')
	{
		$BoardInfo      = TextDB_SearchData('Board','TEXTDB_HID',$bid);  //获取板块信息
		$Nav[0]['ID']   = $bid;
		$Nav[0]['Url']  = 'viewboard.php?bid='.$bid;  //获取板块ID
		$Nav[0]['Name'] = $BoardInfo[0]['分类名称']; //获取板块名称

		$ForumInfo = TextDB_SearchData('Forum_B'.$bid);
		if($ForumInfo)
		{
			foreach($ForumInfo as $K=>$Forum)
			{
				$F_ID                         = $Forum['TEXTDB_HID'];
				$Nav[0]['SubNav'][$K]['Url']  = 'viewforum.php?bid='.$bid.'&fid='.$F_ID;
				$Nav[0]['SubNav'][$K]['Name'] = $Forum['论坛名称'];
			}
		}
	}

	//如果浏览论坛，主题，输出论坛信息
	if(_NavLV_ == 'ViewForum' || _NavLV_ == 'ViewTopic')
	{
		$ForumInfo      = TextDB_SearchData('Forum_B'.$bid,'TEXTDB_HID',$fid);
		$Nav[1]['ID']   = $fid;
		$Nav[1]['Url']  = 'viewforum.php?bid='.$bid.'&fid='.$fid;
		$Nav[1]['Name'] = $ForumInfo[0]['论坛名称'];
	}

	//如果浏览主题 输出主题
	if(_NavLV_ == 'ViewTopic')
	{
		$TopicInfo      = TextDB_SearchData('Topic_B'.$bid.'_F'.$fid,'TEXTDB_HID',$tid);  //获取主题信息
		$Nav[2]['ID']   = $tid;
		$Nav[2]['Url']  = 'viewtopic.php?bid='.$bid.'&fid='.$fid.'&tid='.$tid;
		$Nav[2]['Name'] = $TopicInfo[0]['主题'];
	}

	//如果注册 输出注册页
	if(_NavLV_ == 'Register')
	{
		$Nav[0]['ID']   = 0;
		$Nav[0]['Url']  = 'register.php';
		$Nav[0]['Name'] = '新用户注册';
	}

	//如果注册 输出注册页
	if(_NavLV_ == 'MemberLogin' || _NavLV_ == 'GetPasswd')
	{
		$Nav[0]['ID']   = 0;
		$Nav[0]['Url']  = 'login.php';
		$Nav[0]['Name'] = '用户登陆';
		$Nav[0]['SubNav'][0]['Url']   = 'getpasswd.php';
		$Nav[0]['SubNav'][0]['Name']  = '找回密码';
	}

	//如果页面是公取回密码页
	if(_NavLV_ == 'GetPasswd')
	{
		$Nav[1]['ID']   = 1;
		$Nav[1]['Url']  = 'getpasswd.php';
		$Nav[1]['Name'] = '取回密码';
	}

	//如果页面是公告页
	if(_NavLV_ == 'Announcements')
	{
		$Nav[0]['ID']     = 0;
		$Nav[0]['Url']    = 'announcements.php';
		$Nav[0]['Name']   = '论坛公告';
		define('_ForumAnnoDisplay_',false);  //不显示公告
	}
	else
	{
		define('_ForumAnnoDisplay_',true);  //显示公告
	}

	if(_NavLV_ == 'PROFile' && _LoginID_ != $uid)  //如果会员页面
	{
		$Nav[0]['ID']   = 0;
		$Nav[0]['Url']  = 'profile.php?action=list';
		$Nav[0]['Name'] = '会员列表';

		if(!empty($uid))
		{
			//获取用户信息
			$MemberInfo = TextDB_SearchData('Members','TEXTDB_HID',$uid);
			$Member     = $MemberInfo[0];

			$Nav[1]['ID']   = $uid;
			$Nav[1]['Url']  = 'profile.php?action=view&uid='.$uid;
			$Nav[1]['Name'] = '会员【'.$Member['昵称'].'】的信息';

			if(_UserLogin_)  //如果登录
			{
				$Nav[1]['SubNav'][0]['Url']  = 'profile.php?action=mbox&toname='.$Member['昵称'].'&uid='._LoginID_;
				$Nav[1]['SubNav'][0]['Name'] = '发送信息';
				$LoginMemberInfo   = TextDB_SearchData('Members','TEXTDB_HID',_LoginID_);
				$LoginMember       = $LoginMemberInfo[0];

				//如果不存在好友名单里显示加入好友菜单
				if(!strstr($LoginMember['好友'],$Member['昵称']))
				{
					$Nav[1]['SubNav'][1]['Url']  = 'profile.php?action=addFir&firid='.$Member[0];
					$Nav[1]['SubNav'][1]['Name'] = '加为好友';
				}
			}
		}
	}

	//用户本身
	if(_NavLV_ == 'PROFile' && _LoginID_ == $uid && _UserLogin_)
	{
		$Nav[0]['ID']   = 0;
		$Nav[0]['Url']  = 'profile.php?action=view&uid='.$uid;
		$Nav[0]['Name'] = '控制面板';
	}
	elseif((_NavLV_ == 'PROFile' && $action == 'list') || (_NavLV_ == 'PROFile' && $action == 'view' && empty($uid))) //非用户本人
	{
		$Nav[0]['ID']   = 0;
		$Nav[0]['Url']  = 'profile.php?action=list';
		$Nav[0]['Name'] = '会员列表';
	}


	//设置公告，统计
	$AnnouncementsInfo = TextDB_SearchData('Announcements',NULL,NULL,'desc',NULL,0,1);  //获取公告信息
	if($AnnouncementsInfo)  //如果最后一条信息存在
	{
		define('_AnnouncementsUrl_','announcements.php#'.$AnnouncementsInfo[0][0]);
		define('_AnnouncementsTitle_',$AnnouncementsInfo[0]['标题']);
		define('_AnnouncementsContent_',$AnnouncementsInfo[0]['内容']);
		define('_AnnouncementsDate_',date('Y-m-d H:i:s',$AnnouncementsInfo[0]['发表时间']));
	}
	else  //如果最后一条信息不存在
	{
		define('_AnnouncementsUrl_','javascript:;');
		define('_AnnouncementsTitle_','暂无公告');
		define('_AnnouncementsContent_','');
		define('_AnnouncementsDate_',false);
	}

	//更新缓存库
	UpdateCache('昨日');

	//更新论坛缓存
	UpdateForumCache();

	//获取缓存信息
	$CacheInfo = TextDB_SearchData('ForumCache');
	if($CacheInfo)
	{
		foreach($CacheInfo as $Cache)
		{
			if(isset($Cache['项目名']))
			{
				switch($Cache['项目名'])
				{
					case '今日':
						define('_reTopicToday_',$Cache['数值']);
						break;
					case '昨日':
						define('_reTopicYesterday_',$Cache['数值']);
						break;
					case '最多发帖':
						define('_reTopicMax_',$Cache['数值']);
						define('_TopicMaxDate_',date('Y-m-d H:i:s',$Cache['创建时间']));
						break;
					case '论坛':
						define('_Forum_',$Cache['数值']);
						break;
					case '主题':
						define('_Topic_',$Cache['数值']);
						break;
					case '帖子':
						define('_reTopic_',$Cache['数值']);
						break;
					case '会员':
						define('_regMember_',$Cache['数值']);
						break;
					case '最新注册':
						$CacheVal = explode('',$Cache['数值']);
						if(empty($CacheVal[0]))
						{
							define('_NewRegUrl_','javascript:;');
							define('_NewRegName_','没人注册');
						}
						else
						{
							define('_NewRegUrl_','profile.php?action=view&uid='.$CacheVal[0]);
							define('_NewRegName_',$CacheVal[1]);
						}
						break;
				}
			}
		}
	}

	//统计数据
	if(_NavLV_=='Index')  //如果页面不是首页
	{
		define('_ForumStatDisplay_',true);  //显示统计
	}
	else
	{
		define('_ForumStatDisplay_',false);  //不显示统计
	}

	//快速登录框
	if(_UserLogin_ || strstr($_SERVER['REQUEST_URI'],'register.php') || strstr($_SERVER['REQUEST_URI'],'login.php') || strstr($_SERVER['REQUEST_URI'],'getpasswd.php') || !_ShowMegBox_)    //登录或者注册也则不显示快速登陆框
	{
		define('_QuickLoginDisplay_',false);  //不显示快速登录
	}
	else  //如果没有登录
	{
		define('_QuickLoginDisplay_',true);  //显示快速登录
	}

	if(_OnlineDisplay_)
	{
		$OnlineDisplay     = "show";
		$StatTime          = _StatTime_;

		$LoginIP           = _LoginIP_;
		$LoginOS           = _LoginOS_;
		$LastLoginIP       = _LastLoginIP_;
		$LoginBrowser      = _LoginBrowser_;
		$LastLoginDateTime = _LastLoginDate_;

		//在线图标
		//登陆者的信息
		$OLIcon['LoginLevel']['IMG']   = $OnlineGroup[_LoginJobs_][2];
		$OLIcon['LoginLevel']['Title'] = $OnlineGroup[_LoginJobs_][1];
		$OLIcon['LoginLevel']['Alt']   = $OnlineGroup[_LoginJobs_][0];

		//游客的信息
		$OLIcon['Guest']['IMG']        = $OnlineGroup[0][2];
		$OLIcon['Guest']['Title']      = $OnlineGroup[0][1];
		$OLIcon['Guest']['Alt']        = $OnlineGroup[0][0];

		//论坛管理员的信息
		$OLIcon['ForumAdmin']['IMG']   = $OnlineGroup[1][2];
		$OLIcon['ForumAdmin']['Title'] = $OnlineGroup[1][1];
		$OLIcon['ForumAdmin']['Alt']   = $OnlineGroup[1][0];

		//论坛超级管理员的信息
		$OLIcon['ForumRoot']['IMG']    = $OnlineGroup[2][2];
		$OLIcon['ForumRoot']['Title']  = $OnlineGroup[2][1];
		$OLIcon['ForumRoot']['Alt']    = $OnlineGroup[2][0];

		//论坛板块管理员的信息
		$OLIcon['BoardRoot']['IMG']    = $OnlineGroup[3][2];
		$OLIcon['BoardRoot']['Title']  = $OnlineGroup[3][1];
		$OLIcon['BoardRoot']['Alt']    = $OnlineGroup[3][0];

		//论坛版主管理员的信息
		$OLIcon['Root']['IMG']         = $OnlineGroup[4][2];
		$OLIcon['Root']['Title']       = $OnlineGroup[4][1];
		$OLIcon['Root']['Alt']         = $OnlineGroup[4][0];

		//论坛普通用户的信息
		$OLIcon['User']['IMG']         = $OnlineGroup[5][2];
		$OLIcon['User']['Title']       = $OnlineGroup[5][1];
		$OLIcon['User']['Alt']         = $OnlineGroup[5][0];

		//获取在线数据
		$OnlineInfo = TextDB_SearchData("OnlineInfo");

		$OnlineUser  = 0;
		$OnlineGuest = 0;
		if($OnlineInfo)
		{
			foreach($OnlineInfo as $K=>$OI)
			{
				//获取数据信息
				$Online[$K]["IMG_OnlineJobs"]   = $OnlineGroup[$OI["岗位"]][2];
				$Online[$K]["Alt_OnlineJobs"]   = $OnlineGroup[$OI["岗位"]][0];
				$Online[$K]["Title_OnlineJobs"] = $OnlineGroup[$OI["岗位"]][1];

				if($OI["岗位"])
				{
					$Online[$K]["OnlineUrl"] = "profile.php?action=view&uid=".$OI["用户编号"];
					$OnlineUser++;
				}
				else
				{
					$Online[$K]["OnlineUrl"] = "javascript:;";
					$OnlineGuest++;
				}
				$Online[$K]["OnlineName"]    = $OI["名称"];
				$Online[$K]["OnlineLevel"]   = $OI["等级"];
				$Online[$K]["OnlineOS"]      = $OI["操作系统"];
				$Online[$K]["OnlineBrowser"] = $OI["浏览器"];
				$Online[$K]["OnlineIP"]      = ENIP($OI["访问IP"],1100);
				$Online[$K]["OnlineDate"]    = date('Y-m-d',$OI["访问时间"]);
				$Online[$K]["OnlinePOS"]     = $OI["位置"];
			}
		}
		unset($OnlineInfo);

		$OnlineMaxCache = TextDB_SearchData("ForumCache","项目名","最高在线");
		$OnlineMax      = $OnlineMaxCache[0]["数值"];
	}

	//如果显示链接模块和下拉导航菜单等于Index
	if(_NavLV_=="Index" && _LinkDisplay_)
	{
		ReWriteLinks();

		$LinkDisplay = "show";

		//获取文本链接数据
		$TextLinks = TextDB_SearchData("TextLinks");
		if($TextLinks)
		{
			//输出链接
			foreach($TextLinks as $K=>$TL)
			{
				//获取链接信息
				$TLinks[$K]["Url"]   = $TL["网站地址"];
				$TLinks[$K]["title"] = $TL["网站描述"];
				$TLinks[$K]["Name"]  = $TL["网站名称"];
			}

		}
		unset($TextLinks);

		//获取图片链接数据
		$PicLinks = TextDB_SearchData("PicLinks");
		if($PicLinks)
		{
			foreach($PicLinks as $K=>$PL)
			{
				//获取链接信息
				$PLinks[$K]["ID"]    = $K;
				$PLinks[$K]["Url"]   = $PL["网站地址"];
				$PLinks[$K]["title"] = $PL["网站描述"];
			}
		}
		unset($PicLinks);
	}

	//检测论坛管理操作
	ForumAdminAction();

?>